package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.DutyShift;
import com.ruoyi.system.service.IDutyShiftService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 值班信息Controller
 * 
 * @author txy
 * @date 2022-02-09
 */
@RestController
@RequestMapping("/system/shift")
public class DutyShiftController extends BaseController
{
    @Autowired
    private IDutyShiftService dutyShiftService;

    /**
     * 查询值班信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:shift:list')")
    @GetMapping("/list")
    public TableDataInfo list(DutyShift dutyShift)
    {
        startPage();
        List<DutyShift> list = dutyShiftService.selectDutyShiftList(dutyShift);
        return getDataTable(list);
    }


    /**
     * 查询当前值班人
     */
    @PreAuthorize("@ss.hasPermi('system:shift:list')")
    @GetMapping("/current")
    public AjaxResult currentPerson(DutyShift dutyShift)
    {
        dutyShift = dutyShiftService.selectCurrentPerson();
        return AjaxResult.success(dutyShift);
    }

    /**
     * 导出值班信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:shift:export')")
    @Log(title = "值班信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DutyShift dutyShift)
    {
        List<DutyShift> list = dutyShiftService.selectDutyShiftList(dutyShift);
        ExcelUtil<DutyShift> util = new ExcelUtil<DutyShift>(DutyShift.class);
        util.exportExcel(response, list, "值班信息数据");
    }

    /**
     * 获取值班信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shift:query')")
    @GetMapping(value = "/{currentShift}")
    public AjaxResult getInfo(@PathVariable("currentShift") String currentShift)
    {
        return AjaxResult.success(dutyShiftService.selectDutyShiftByCurrentShift(currentShift));
    }

    /**
     * 新增值班信息
     */
    @PreAuthorize("@ss.hasPermi('system:shift:add')")
    @Log(title = "值班信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DutyShift dutyShift)
    {
        String currentShift = dutyShift.getCurrentShift();
        if(currentShift == null || "".equals(currentShift)){
            dutyShift.setCurrentShift(UUID.randomUUID().toString());
            dutyShift.setShiftStartTime(new Date());
            return toAjax(dutyShiftService.insertDutyShift(dutyShift));
        }else{
            dutyShift.setCurrentPeople(null);
            return toAjax(dutyShiftService.updateDutyShift(dutyShift));
        }
    }

    /**
     * 交班
     */
    @PreAuthorize("@ss.hasPermi('system:shift:edit')")
    @Log(title = "值班信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DutyShift dutyShift)
    {
        //先更新之前的上班状态未
        DutyShift oldDutyShift = new DutyShift();
        oldDutyShift.setCurrentShift(dutyShift.getCurrentShift());
        oldDutyShift.setShitfEndTime(new Date());
        oldDutyShift.setShiftStatus(dutyShift.getShiftStatus());
        toAjax(dutyShiftService.updateDutyShift(oldDutyShift));
        //新增当前上班人员
        String currentShift = dutyShift.getCurrentPeople();
        String handoverPerson = dutyShift.getHandoverPerson();
        dutyShift.setCurrentShift(UUID.randomUUID().toString());
        dutyShift.setShiftMoney("0");
        dutyShift.setShiftDeposit("0");
        dutyShift.setShiftStartTime(new Date());
        dutyShift.setCurrentPeople(handoverPerson);
        dutyShift.setHandoverPerson(currentShift);
        dutyShift.setShiftStatus("1");
        return toAjax(dutyShiftService.insertDutyShift(dutyShift));
    }

    /**
     * 删除值班信息
     */
    @PreAuthorize("@ss.hasPermi('system:shift:remove')")
    @Log(title = "值班信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{currentShifts}")
    public AjaxResult remove(@PathVariable String[] currentShifts)
    {
        return toAjax(dutyShiftService.deleteDutyShiftByCurrentShifts(currentShifts));
    }
}
